package com.coderman.common.starter.excelport.imports.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.coderman.common.starter.excelport.imports.config.ImportBeanConfig;
import com.coderman.common.starter.excelport.imports.service.GlobalExcelListener;
import com.coderman.common.starter.excelport.imports.service.ImportService;
import com.coderman.common.starter.excelport.imports.service.common.ReadExcelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Description: 导入实现服务
 * date: 2020/9/15 9:10 上午
 *
 * @author fanchunshuai
 * @version 1.0.0
 * @since JDK 1.8
 */
@Service
public class ImportServiceImpl implements ImportService {

    @Autowired
    private GlobalExcelListener globalExcelListener;

    @Autowired
    private ReadExcelService readExcelService;

    @Override
    public  void exeImportSheet(ImportBeanConfig importBeanConfig) throws Exception {

        // 读取部分sheet
        String fileName = importBeanConfig.getFilePath();
        ExcelReader excelReader = EasyExcel.read(fileName).build();
        globalExcelListener.setImportBeanConfig(importBeanConfig);
        globalExcelListener.setImportTitleBean(readExcelService.getImportTitle(importBeanConfig.getT().getClass()));
        globalExcelListener.setImportFieldBeanMap(readExcelService.getMetaFieldColumnMap(importBeanConfig.getT().getClass()));
        // 这里为了简单 所以注册了 同样的head 和Listener 自己使用功能必须不同的Listener
        ReadSheet readSheet1 =
                EasyExcel.readSheet(0).head(importBeanConfig.getT().getClass()).registerReadListener(globalExcelListener).build();
        //设置读取表头跳过行数
        if(importBeanConfig.getSkipRowNum()>=1){
            readSheet1.setHeadRowNumber(importBeanConfig.getSkipRowNum());
        }
        excelReader.read(readSheet1);

        // 这里千万别忘记关闭，读的时候会创建临时文件，到时磁盘会崩的
        excelReader.finish();
    }

    @Override
    public void exeImportMultSheet(List<ImportBeanConfig> importBeanConfigList) throws Exception {
        ExcelReader excelReader = EasyExcel.read(importBeanConfigList.get(0).getFilePath()).build();
        for (int i = 0;i < importBeanConfigList.size();i++){
            ImportBeanConfig importBeanConfig = importBeanConfigList.get(i);

            globalExcelListener.setImportBeanConfig(importBeanConfig);
            globalExcelListener.setImportTitleBean(readExcelService.getImportTitle(importBeanConfig.getT().getClass()));
            globalExcelListener.setImportFieldBeanMap(readExcelService.getMetaFieldColumnMap(importBeanConfig.getT().getClass()));
            ReadSheet readSheet =
                    EasyExcel.readSheet(0).head(importBeanConfig.getT().getClass())
                            .registerReadListener(globalExcelListener)
                            .build();
            if(importBeanConfig.getSkipRowNum()>=1){
                readSheet.setHeadRowNumber(importBeanConfig.getSkipRowNum());
            }
            excelReader.read(readSheet);
        }
        // 这里千万别忘记关闭，读的时候会创建临时文件，到时磁盘会崩的
        excelReader.finish();
    }
}
